﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NJL.WebUsers.Visitor
{
    public partial class MessageBoard : BasePageVisitor
    {
        NJL.BLL.NJL_LEAVE_MESSAGE bllMessage = new BLL.NJL_LEAVE_MESSAGE();
        NJL.Model.NJL_LEAVE_MESSAGE modelMessage = new Model.NJL_LEAVE_MESSAGE();
        NJL.BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
        NJL.Model.NJL_PERSON modelPerson;
        NJL.Model.NJL_USER modelUserHost;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
           {

                modelUserHost = bllUser.GetModel(Request.QueryString["uid"]);
                Page.Title = "留言-" + modelUserHost.USER_NAME + "的乡间小屋";
                bind();
            }
        }
        void bind()
        {
            #region 留言
            DataSet dsMessage = bllMessage.GetList("NJL_USER_ID='" + Request.QueryString["uid"] + "' and LEAVE_MESSAGE_TYPE_CODE=1  ");
            if (dsMessage.Tables[0].Rows.Count > 10)
            {
                pager.InnerHtml = Royalsoft.Common.Pager.GetPageNum(dsMessage, rptCommentList, 10, new string[] { "uid" });
            }
            else
            {
                rptCommentList.DataSource = dsMessage;
                rptCommentList.DataBind();
            }
            litCommentCount.Text = dsMessage.Tables[0].Rows.Count.ToString();
            #endregion
        }
        protected void btnComment_Click(object sender, EventArgs e)
        {
            if (UserId == "" || UserType == "-1")
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "请登录后再发表", "/Login.aspx?url=" + Server.UrlEncode(Request.RawUrl));
                return;
            }
            else
            {
                if (Session["CheckCode"] != null)
                {
                    if (txtValidate.Text.Trim().ToLower() != Session["CheckCode"].ToString().ToLower())
                    {
                        Royalsoft.Common.MessageBox.Show(this, "请输入正确的验证码");
                        return;
                    }
                    modelMessage.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                    modelMessage.LEAVE_MESSAGE_TYPE_CODE = 1;
                    modelMessage.LEAVE_MESSAGE = txtComment.Text.Trim();
                    modelMessage.NJL_USER_ID = Request.QueryString["uid"].ToString();
                    modelMessage.CREATE_USER_ID = new BasePage().UserId;
                    modelMessage.CREATE_USER_NAME = new BasePage().UserName;
                    modelMessage.CREATE_DATE_TIME = DateTime.Now;
                    bllMessage.Add(modelMessage);
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "留言成功", Request.RawUrl);
                    modelPerson = bllPerson.GetModelByUserId(Request.QueryString["uid"]);
                    if (bllPerson.updateCommentHitAndDate(modelPerson.ID))
                    {

                    }
                }
                else
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "连接系统超时", "" + Request.RawUrl);
                }
            }

        }
        protected void rptCommentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView grv = (DataRowView)e.Item.DataItem;
                string userId = grv["CREATE_USER_ID"].ToString();
                Image imgUserHead = e.Item.FindControl("imgUserHead") as Image;
                modelPerson = bllPerson.GetModelByUserId(userId);
                if (modelPerson != null)
                {
                    if (modelPerson.IMAGE_CODE != "")
                    {
                        imgUserHead.ImageUrl = GetImageUrl(modelPerson.IMAGE_CODE, "U1");
                        imgUserHead.BorderWidth = 1;
                    }
                    else
                    {
                        imgUserHead.ImageUrl = "/Images/avatar.jpg";
                        imgUserHead.BorderWidth = 1;
                    }
                }
                LinkButton lbtn = e.Item.FindControl("lbtnDel") as LinkButton;
                if (UserId != "" && UserId.Equals(Request.QueryString["uid"]))
                {
                    lbtn.Visible = true;
                }
                else
                {
                    lbtn.Visible = false;
                }
            }
        }

        protected void rptCommentList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                if (bllMessage.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
    }
}